package com.huika.dealontop.control.nearby.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huika.dealontop.R;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.control.base.extend.BaseViewHolder;
import com.huika.dealontop.control.nearby.activity.MerchantDetailActivity;
import com.huika.dealontop.control.shops.activity.ProductDetailsActivity1;
import com.huika.dealontop.control.timelimit.activity.MemberEvaluateActicvity;
import com.huika.dealontop.support.domain.OtherGrouponBean;
import com.huika.dealontop.support.domain.UserEvaluationItemBean;
import com.huika.dealontop.utils.DateTimeTool;
import com.huika.dealontop.utils.ImageLoaderHelper;
import com.huika.dealontop.utils.UIUtils;
import com.huika.dealontop.views.RoundImageView;
import com.huika.huikalib.utils.MoneyShowTool;
import com.huika.huikalib.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huhao on 2015/11/24.
 */
public class MerchantDetailAdapter extends BaseAdapter {
    private Context mContext;
    public List<UserEvaluationItemBean> mUserEvaluationList = new ArrayList<UserEvaluationItemBean>();
    public List<OtherGrouponBean> mOtherGrouponList = new ArrayList<OtherGrouponBean>();
    private Object grouponHolder;
    private MerchantDetailActivity merchantDetailActivity;
    public boolean hasMoreGroupon = false;

    public MerchantDetailAdapter(Context mContext, MerchantDetailActivity merchantDetailActivity) {
        this.mContext = mContext;
        this.merchantDetailActivity = merchantDetailActivity;
    }

    @Override
    public int getCount() {
        int count = getOtherGrouponCount();

        count += getEvaluationCount();

        return count + 1; //中间的更多条目 和 。。
    }

    public int getOtherGrouponCount() {
        if (mOtherGrouponList != null) {
            return mOtherGrouponList.size();
        }
        return 0;
    }

    public int getEvaluationCount() {
        if (mUserEvaluationList != null) {
            return mUserEvaluationList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseViewHolder holder = null;
        Object data = null;
        if (convertView == null) {
            holder = new ItemHolder(mContext);
            convertView = holder.getRootView();
        } else {
            holder = (BaseViewHolder) convertView.getTag();
        }

        if (position >= 0 && position < getOtherGrouponCount()) {
            data = mOtherGrouponList.get(position);
        } else if (position == getOtherGrouponCount()) { //更多团购按钮
            //更多团购按钮
            data = hasMoreGroupon;
        } else {
            data = mUserEvaluationList.get(position - getOtherGrouponCount() - 1);
        }
        holder.setData(data);
        return convertView;
    }

    public class ItemHolder extends BaseViewHolder<Object> {
        private RelativeLayout mll_merchant_product;
        private LinearLayout mll_merchant_more, mll_merchant_userpingjia, mll_merchant_pingjia;
        private ImageView iv_icon;
        private TextView tv_name, tv_price, tv_zhekounum, tv_pingfennum, tv_merchant_more, tv_merchant_content,
                tv_merchant_time, tv_merchant_user_name, tv_merchant_score, tv_merchant_distance;
        private RatingBar rb_pingfen, rb_merchant_score;
        private RoundImageView iv_merchant_user_icon;
        private View mview;

        public ItemHolder(Context context) {
            super(context);
        }

        @Override
        protected View initView() {
            View view = View.inflate(UIUtils.getContext(), R.layout.item_business_lineview, null);
            mll_merchant_product = (RelativeLayout) view.findViewById(R.id.ll_merchant_product);
            mll_merchant_more = (LinearLayout) view.findViewById(R.id.ll_merchant_more);
            mll_merchant_pingjia = (LinearLayout) view.findViewById(R.id.mll_merchant_pingjia);
            mll_merchant_userpingjia = (LinearLayout) view.findViewById(R.id.ll_merchant_userpingjia);
            iv_icon = (ImageView) view.findViewById(R.id.project_iv_icon);
            tv_name = (TextView) view.findViewById(R.id.project_tv_name);
            tv_price = (TextView) view.findViewById(R.id.project_tv_price);
            tv_zhekounum = (TextView) view.findViewById(R.id.project_tv_zhekounum);
            tv_pingfennum = (TextView) view.findViewById(R.id.project_tv_pingfennum);
            tv_merchant_more = (TextView) view.findViewById(R.id.tv_merchant_more);
            rb_pingfen = (RatingBar) view.findViewById(R.id.project_rb_pingfen);
            iv_merchant_user_icon = (RoundImageView) view.findViewById(R.id.iv_merchant_user_icon);
            rb_merchant_score = (RatingBar) view.findViewById(R.id.rb_merchant_score);
            tv_merchant_score = (TextView) view.findViewById(R.id.tv_merchant_score);
            tv_merchant_content = (TextView) view.findViewById(R.id.tv_merchant_content);
            tv_merchant_time = (TextView) view.findViewById(R.id.tv_merchant_times);
            tv_merchant_user_name = (TextView) view.findViewById(R.id.tv_merchant_user_name);
            mview = (View) view.findViewById(R.id.view);
            return view;
        }

        private OtherGrouponBean goupon;
        private Boolean more;
        private UserEvaluationItemBean evaluationBean;

        @Override
        protected void refreshView(final Object data) {
            mll_merchant_product.setVisibility(View.GONE);
            mll_merchant_more.setVisibility(View.GONE);
            mll_merchant_userpingjia.setVisibility(View.GONE);
            if (mUserEvaluationList.size()>0) {
                mll_merchant_pingjia.setVisibility(View.VISIBLE);
                mview.setVisibility(View.VISIBLE);
            } else {
                mll_merchant_pingjia.setVisibility(View.GONE);
                mview.setVisibility(View.GONE);
            }
            if (data instanceof OtherGrouponBean) {
                goupon = (OtherGrouponBean) data;
                mll_merchant_product.setVisibility(View.VISIBLE);
                if (StringUtils.isEmpty(goupon.getProductPicUrl())) {
                    iv_icon.setImageResource(R.mipmap.morenpic);
                } else {
                    ImageLoaderHelper.displayImage(goupon.getProductPicUrl(), iv_icon);
                }
                tv_name.setText(goupon.getProductName());
                tv_price.setText(Math.round(goupon.getSalesPrice()) + "");
                tv_zhekounum.setText(Math.round(goupon.getDiscount()) + "%");
                if (Math.round(goupon.getProductRate()) == 0) {
                    rb_pingfen.setRating(5f);
                    tv_pingfennum.setText("5.0");
                } else {
                    tv_pingfennum.setText(Float.valueOf(MoneyShowTool.formatOne(goupon.getProductRate())) + "");
                    rb_pingfen.setRating(Float.valueOf(MoneyShowTool.formatOne(goupon.getProductRate())));
                }
                mll_merchant_product.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String mProductID = goupon.getProductId() + "";
                        Intent intent = new Intent();
                        intent.putExtra(ProductDetailsActivity1.PRODUCT_ID, mProductID);
                        intent.putExtra(ProductDetailsActivity1.PRODUCT_IMG, goupon.getProductPicUrl());
                        intent.setClass(merchantDetailActivity, ProductDetailsActivity1.class);
                        merchantDetailActivity.startActivity(intent);
                    }
                });


            } else if (data instanceof UserEvaluationItemBean) {
                evaluationBean = (UserEvaluationItemBean) data;
                mll_merchant_userpingjia.setVisibility(View.VISIBLE);
                if (StringUtils.isEmpty(evaluationBean.getHeadImage())) {
                    iv_merchant_user_icon.setImageResource(R.mipmap.my_infomation_user_icon);
                } else {
                    ImageLoader.getInstance().displayImage(evaluationBean.getHeadImage(), iv_merchant_user_icon);
                }
                tv_merchant_user_name.setText(evaluationBean.getNickName());
                if (StringUtils
                        .isEmpty(evaluationBean.getEvalScore())) {
                    rb_merchant_score.setRating(5f);
                    tv_merchant_score.setText("5.0");
                } else {
                    rb_merchant_score.setRating(Float.valueOf(MoneyShowTool.formatOne(Double.valueOf(evaluationBean
                            .getEvalScore()))));
                    tv_merchant_score.setText(MoneyShowTool.formatOne(Double.valueOf(evaluationBean
                            .getEvalScore())) + "");
                }
                tv_merchant_content.setText(evaluationBean.getEvalContent());
                tv_merchant_time.setText(String.format(DateTimeTool.dmyDate(evaluationBean.getEvalTime() * 1000)) + "");
                mll_merchant_userpingjia.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String mEvaluateID = evaluationBean.getEvaluateID() + "";
                        Intent intent = new Intent();
                        intent.putExtra(Constants.EVALUATE_ID, mEvaluateID);
                        intent.setClass(merchantDetailActivity, MemberEvaluateActicvity.class);
                        merchantDetailActivity.startActivity(intent);
                    }
                });

            } else if (data instanceof Boolean) {
                //更对
                more = (Boolean) data;
                mll_merchant_more.setVisibility(View.VISIBLE);
                mll_merchant_more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (more = true) {
                            merchantDetailActivity.getOtherGrouponListMore();

                        }
                    }
                });
                tv_merchant_more.setVisibility(more == true ? View.VISIBLE : View.GONE);
            }
        }
    }

}
